const express = require('express')
const router = express.Router()
const db = require('../../utils/db')
router.post('/patientManagementList', (req, res) => {
  const { page, pageSize, createTimeStart, createTimeEnd, name, tel } = req.body
  let select = `SELECT * FROM patient where patient.isDelete=0 AND s_name LIKE '%${name}%' AND s_tel LIKE '%${tel}%'`
  let sel = `SELECT * FROM patient JOIN doctor ON patient.s_u_id=doctor.u_id AND patient.isDelete=0 WHERE s_name LIKE '%${name}%' AND s_tel LIKE '%${tel}%' LIMIT ${
    (page - 1) * pageSize
  },${pageSize}`
  if (createTimeStart && createTimeEnd) {
    select = `SELECT * FROM patient WHERE patient.isDelete=0 AND s_name LIKE '%${name}%' AND s_tel LIKE '%${tel}%' AND s_createTime >='${createTimeStart}' AND s_createTime <='${createTimeEnd}'`
    sel = `SELECT * FROM patient JOIN doctor ON patient.s_u_id=doctor.u_id AND patient.isDelete=0 WHERE s_name LIKE '%${name}%' AND s_tel LIKE '%${tel}%' AND s_createTime >='${createTimeStart}' AND s_createTime <='${createTimeEnd}' LIMIT ${
      (page - 1) * pageSize
    },${pageSize}`
  }
  db.query(select, (error, resu) => {
    if (error) {
    } else {
      db.query(sel, (err, result) => {
        if (err) {
          res.send({ err, msg: 'error' })
        } else {
          res.send({ data: result, msg: 'success', total: resu.length })
        }
      })
    }
  })
})
router.post('/patientManagementDelete', (req, res) => {
  let { id } = req.body
  let sel = `update patient set isDelete=1 where s_id=${id}`
  db.query(sel, (err, data) => {
    if (err) {
      res.send({ err, msg: 'error' })
    } else {
      res.send({ msg: 'success' })
    }
  })
})
const toDate = t => new Date(t).toLocaleString('zh-CN', { dateStyle: 'short' })
router.post('/patientManagementAdd', (req, res) => {
  let {
    s_name,
    s_sex,
    s_age,
    s_tel,
    s_createTime,
    s_u_id,
    s_cardID,
    s_marry,
    s_address
  } = req.body
  let sel = `INSERT INTO patient VALUES (NULL,'${s_name}',${s_sex},${s_age},${s_tel},'${toDate(
    s_createTime
  )}',${s_u_id},${s_cardID},${s_marry},'${s_address}',DEFAULT,DEFAULT,'无','无')`
  db.query(sel, (err, data) => {
    if (err) {
      res.send({ err, msg: 'error' })
    } else {
      res.send({ msg: 'success' })
    }
  })
})
router.post('/patientManagementPreEdit', (req, res) => {
  let { id } = req.body
  let sel = `SELECT * FROM patient JOIN doctor ON patient.s_u_id=doctor.u_id where s_id=${id}`
  db.query(sel, (err, result) => {
    if (err) {
      res.send({ err, msg: 'error' })
    } else {
      res.send({ data: result, msg: 'success', total: result.length })
    }
  })
})
router.post('/patientManagementUpdate', (req, res) => {
  let {
    s_id,
    s_name,
    s_sex,
    s_age,
    s_tel,
    s_createTime,
    s_u_id,
    s_cardID,
    s_marry,
    s_address
  } = req.body
  let sel = `UPDATE patient SET s_name='${s_name}',s_sex=${s_sex},s_age=${s_age},s_tel=${s_tel},s_createTime='${s_createTime}',s_u_id=${s_u_id},s_cardID=${s_cardID},s_marry=${s_marry},s_address='${s_address}' WHERE s_id=${s_id}`
  db.query(sel, (err, result) => {
    if (err) {
      res.send({ err, msg: 'error' })
    } else {
      res.send({ msg: 'success' })
    }
  })
})
//患者年龄查询
router.get('/getPatientAges', (req, res) => {
  const sql = `SELECT s_age AS age FROM patient JOIN register_record WHERE s_id=reg_s_id;`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: '查询数据库失败', err })
    } else {
      const ages = Array(9).fill(0)
      data.forEach(v => {
        if (v.age >= 80) {
          ages[8]++
        } else {
          let index = Math.floor(v.age / 10)
          ages[index]++
        }
      })
      res.send({ error: 0, msg: '查询成功', data: ages })
    }
  })
})
module.exports = router
